<template>
  <div>
    <el-button type="danger" @click="batchDelete">批量删除</el-button>
    <el-tree :data="menus"
             :props="defaultProps"
             accordion
             show-checkbox
             node-key="id"
             :expand-on-click-node="false"
             highlight-current
             :default-expanded-keys="expandedKey"
             ref="menuTree"
             v-loading="loading"
             >
    <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            @click="() => append(node, data)" v-if='node.level <= 2'>
            新增
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="edit(node, data)">
            修改
          </el-button>
          <el-buttonR
            type="text"
            size="mini"
            @click="() => remove(node, data)" v-if="node.isLeaf">
            删除
          </el-buttonR>
        </span>
      </span>
    </el-tree>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: 'category',
    data () {
      return {
        loading: true,
        title: '',
        category: {name: '', parentId: 0, isParent: 0, sort: 0, status: 0},
        menus: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        expandedKey: [],
        dialogVisible: false,
        isAdd: true
      }
    },
    methods: {
      batchDelete () {
        let ids = []
        let checkedNodes = this.$refs.menuTree.getCheckedNodes()
        checkedNodes.forEach(node => {
          ids.push(node.id)
        })
        this.$confirm(`是否批量删除【${ids}】菜单?`, '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/item/category/delete'),
            method: 'delete',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getMenus()
          })
        })
      },
      edit (node, data) {
        this.title = '修改'
        this.flag = false
        this.category = node.data
        this.dialogVisible = true
      },
      addCategory () {
        let url = this.flag ? '/item/category/save' : '/item/category/update'
        let method = this.flag ? 'post' : 'put'
        this.$http({
          url: this.$http.adornUrl(url),
          method: method,
          data: this.$http.adornData(this.category, false)
        }).then(({data}) => {
          this.dialogVisible = false
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.getMenus()
          this.expandedKey = [this.category.parentId]
        }).catch(resp => {
          this.dialogVisible = false
          this.$message({
            type: 'warning',
            message: '添加失败!'
          })
        })
      },
      getMenus () {
        this.category.name = ''
        this.$http({
          url: this.$http.adornUrl('/item/category/list/tree'),
          method: 'get'
        }).then(({data}) => {
          this.menus = data.data
        })
        this.loading = false
      },
      append (node, data) {
        this.title = '新增'
        this.flag = true
        this.category.parentId = data.id
        this.category.isParent = node.level + 1 < 3 ? 1 : 0
        this.category.sort = 0
        this.category.status = 0
        this.dialogVisible = true
      },
      remove (node, data) {
        let ids = [data.id]
        this.$confirm(`是否删除【${data.name}】菜单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/item/category/delete'),
            method: 'delete',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getMenus()
            this.expandedKey = [node.parent.data.id]
          })
        })
      }

    },
    created () {
      this.getMenus()
    }

  }
</script>

<style scoped>

</style>
